#
#    Copyright (C) 2016 Francisco Javier Parra <franparpe@openmailbox.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QVBoxLayout

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

from PyQt5 import QtCore

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s


class JerkGraphDialog(QDialog):
    """This window contains the position plot

    """
    def __init__(self, parent):
        super(JerkGraphDialog, self).__init__(parent,
                                              QtCore.Qt.Window)
        self.setGeometry(0, 0, 600, 600)
        # self.setTitle(_fromUtf8("Position plot"))
        self.graphPos = Figure()
        self.canvasGraphPos = FigureCanvas(self.graphPos)
        self.toolBarGraphPos = NavigationToolbar(self.canvasGraphPos,
                                                 self)
        self.HBox_lay = QVBoxLayout(self)
        # Place items in the layout
        self.HBox_lay.addWidget(self.canvasGraphPos)
        self.HBox_lay.addWidget(self.toolBarGraphPos)

        self.plotManipulator = self.graphPos.add_subplot(111)
        self.plotManipulator.set_title(_fromUtf8("Jerk"))
        # self.plotManipulator.grid(b='on')
        self.plotManipulator.set_xlabel(_fromUtf8("Angle of rotation [º]"))
        self.plotManipulator.set_ylabel(_fromUtf8("J [mm/s³]"))
        # activate the grid
        self.plotManipulator.grid(True)

    def Graph(self, theCam, otherAbci=[], otherOrdi=[]):
        """Plots the position if a cam object
        is placed as a parameter
        """
        self.plotManipulator.clear()
        self.plotManipulator.set_title(_fromUtf8("Jerk"))
        self.plotManipulator.set_xlabel(_fromUtf8("Angle of rotation [º]"))
        self.plotManipulator.set_ylabel(_fromUtf8("J [mm/s³]"))
        # activate the grid
        self.plotManipulator.grid(True)

        # Time to plot
        for section in theCam.sections:
            section.CreateYaxisOverAcceleration()
            self.plotManipulator.plot(section.abcissaeValues,
                                      section.ordinateOverAcceleration, "-")
        if len(otherAbci) != 0:
            self.plotManipulator.plot(otherAbci, otherOrdi, "-")

        self.canvasGraphPos.draw()
        self.exec_()
